import axios from 'axios'

// 创建专门用于ASR的axios实例，不需要认证
const asrService = axios.create({
  baseURL: 'http://localhost:8080/api',
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  }
})

// 请求拦截器
asrService.interceptors.request.use(
  config => {
    console.log('ASR API请求:', config.method?.toUpperCase(), config.url)
    console.log('请求数据类型:', config.data?.constructor?.name)
    console.log('请求头:', config.headers)
    
    // 如果是FormData，确保不设置Content-Type，让浏览器自动设置
    if (config.data instanceof FormData) {
      delete config.headers['Content-Type']
    }
    
    return config
  },
  error => {
    console.error('ASR API请求错误:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
asrService.interceptors.response.use(
  response => {
    console.log('ASR API响应:', response.status, response.data)
    return response.data
  },
  error => {
    console.error('ASR API响应错误:', error)
    let message = '请求失败'
    
    if (error.response) {
      message = `请求失败: ${error.response.status} ${error.response.statusText}`
    } else if (error.request) {
      message = '网络连接失败，请检查后端服务是否启动'
    } else {
      message = error.message
    }
    
    return Promise.reject(new Error(message))
  }
)

export default asrService
